﻿using Microsoft.SharePoint;
using SharepointCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Uportal.Shp.Core.Enums;
using Uportal.Shp.Core.Items;
using Uportal.Shp.Core.Model;
using Uportal.Shp.Core.Services;

namespace Uportal.Shp.Core.Common
{
    public class FeedbackManager :IDisposable
    {
        private readonly IQueryWeb _queryWeb;
        NLogLogger _logger = new NLogLogger(@"c:\Temp\Logs\Uportal.FeedbackManager_${shortdate}.log");
        //private readonly IAppPackage _app;
        //private SPListItemCollection 
        

        public FeedbackManager()
        {
            //var sl = SL.GetCurrent();
            //var url = Settings.Get().WebUrl;

            //_queryWeb = WebFactory.Elevated(url);
        }

        public void Dispose()
        {
            _queryWeb.Dispose();
        }

        /// <summary>
        /// Получение всех элементов, котрые не отвечены с прошлой недели
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
       public static IEnumerable<FeedbackItems> GetReport(IQueryWeb web)
        {
            NLogLogger _log = new NLogLogger(@"c:\Temp\Logs\Uportal.FeedbackManager_${shortdate}.log");
            try
            {

                var list = web.GetByUrl<FeedbackItems>(FeedbackToManagmentItem.ListUrl);

                string q = Q.Where(
                                Q.And(
                                    Q.Eq(
                                        Q.FieldRef(Fields.FeedbackToManagment.StateOfQuestion),
                                        Q.Value("Text", EnumResolver.GetStringValue(StateQuestion.Approved)
                                        )),
                                        Q.IsNull(Q.FieldRef(Fields.FeedbackToManagment.DateOfAnswer)
                                        )
                                    )
                                );
                _log.Info(q);

                var items = list.Items(new CamlQuery().Query(q));

                if (items == null) return null;

                foreach (var item in items)
                {
                    _log.Info(item.ParentList.FormUrl(PageType.Display));
                    _log.Info(web.Web.Url);
                }
                return items;

            }
            catch (Exception ex)
            {
                _log.Info(ex.Message);
                return null;
            }
       }

        internal string GetFormFullUrl(SPListItem item, PAGETYPE type)
        {
            return _queryWeb.Web.Url + item.ParentList.Forms[type].ServerRelativeUrl + "?ID=" + item.ID.ToString();
        }

        public static string SubjForModerator { get{ return "Утверждение вопроса"; } }

        public static string SubjForNamager { get { return "Необходим ответ на вопрос"; } }

    }
}